//
//  IslanDescribCtll.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/3/2.
//  Copyright © 2016年 game. All rights reserved.
//

#import "IslanDescribCtll.h"
#import "AticHeadView.h"
#import "IsLanDecribFootView.h"
#import "IsLanDescribTabView.h"
#import "ImageToController.h"
#import "ClickCommentCtl.h"
#import "IslanInforChangeCtl.h"
#import "IslanShowMoreCtrl.h"
#import "LoginController.h"
#import "OtherInformationCtrl.h"
#import "IslanShowMoreCommentCtrl.h"
#import "IslanOtherWrongController.h"
#import "IslanDetailedLocalCtrl.h"
#import "ChangeLocalCtrl.h"

@interface IslanDescribCtll ()<IsLanDescribTabViewClickDeleget,UpLodeImageDeleget,ClickCommentCtlDeleget>
@property (nonatomic,copy) NSString *NavTitle;
@property (nonatomic,copy) NSString *yiGuan;
@property (nonatomic,copy) NSString *xinxiYouwu;
@property (nonatomic,copy) NSString *chongfu;

@property (nonatomic,strong) MBProgressHUD *MyHUD;
@property (nonatomic,strong) IsLanDescribTabView *mytableview;
@property (nonatomic,strong) NSCache *HudCache;
@end

@implementation IslanDescribCtll

-(void)setIsYuJuDian:(BOOL)isYuJuDian{
    _isYuJuDian = isYuJuDian;
    
    if (_isYuJuDian) {
        self.NavTitle = @"渔具店详情";
        self.yiGuan = @"渔具店已关";
        self.xinxiYouwu = @"渔具店信息有误";
        self.chongfu = @"渔具店重复";
        
    }else{
        self.NavTitle = @"钓场详情";
        self.yiGuan = @"钓场已关";
        self.xinxiYouwu = @"钓场信息有误";
        self.chongfu = @"钓场重复";
    }
}
-(void)CreatNav{    
    self.Navview = (AticHeadView *)[self CreatNavViewWhithTitle:self.NavTitle];
    self.Navview.shareBnt.alpha = 0;
    self.Navview.collectBnt.alpha = 0;
    [self.view addSubview:self.Navview];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.app.myTabBar.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = YES;
    
    self.navigationController.navigationBarHidden = YES;
        [self CreatNav];
}
-(void)CreatFootView{
    IsLanDecribFootView *foot = [[NSBundle mainBundle]loadNibNamed:@"IsLanDecribFootView" owner:nil options:nil].lastObject;
    foot.frame = CGRectMake(0, SCREEN_HEIGHT-50, SCREEN_WIDTH, 50);
    [self.view addSubview:foot];
    
    [foot.chuantuBTn addTarget:self action:@selector(ChuantuClick) forControlEvents:UIControlEventTouchUpInside];
    
    [foot.dianpingbtn addTarget:self action:@selector(DianpingClick) forControlEvents:UIControlEventTouchUpInside];
    
    [foot.baocuoBtn addTarget:self action:@selector(BaocuoClick) forControlEvents:UIControlEventTouchUpInside];
    
    [foot.guanzhuBtn addTarget:self action:@selector(GuanzhuClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.mytableview.GuanzhuBtn = foot.guanzhuBtn;
}
#pragma mark--点击关注
-(void)GuanzhuClick:(UIButton *)btn{
   
    [[Manager sharedManager]RequestWhithURL:GetFormhash AndParameter:nil AndCompletionType:^(id responseObject, DIEError *error) {
        if ([responseObject[@"status"] isEqualToString:@"SUCCESS"]) {
     
            if (![self.app.isLogin isEqualToString:@"YES"]) {
                LoginController *login = [LoginController new];
                [self.navigationController pushViewController:login animated:YES];
                
            }else{
                
                if (!(btn.selected==YES)) {
                    [self getAttentionWhithFormhash:responseObject[@"data"][@"formhash"] AndBtn:btn];
                    btn.selected = YES;

                }else{
                    btn.userInteractionEnabled = NO;
                }
            }
        }
    }];
}

-(void)getAttentionWhithFormhash:(NSString *)formhash AndBtn:(UIButton *)btn{

    NSString *url = islanAttention;
    NSString *iddd = @"d_id";
    if (self.isYuJuDian) {
        url = dianpuAttention;
        iddd = @"y_id";
    }
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic addEntriesFromDictionary: @{iddd:self.ID,@"formhash":formhash}];
    [[Manager sharedManager]ResponseWhithURL:url AndParameter:dic AndCompletionType:^(id responseObject, DIEError *error) {
        
        [Manager GetHudWhithText:responseObject[@"message"] AndFatherView:self.view];
        if ([responseObject[@"status"] isEqualToString:@"SUCCESS"]) {
            btn.selected = YES;
            btn.userInteractionEnabled = NO;
        }
        
    }];
}

#pragma mark---点击传图
-(void)ChuantuClick{
    
    if (![self.app.isLogin isEqualToString:@"YES"]) {
        LoginController *login = [LoginController new];
        [self.navigationController pushViewController:login animated:YES];
    }else{
        
        ImageToController *chuantu = [ImageToController new];
        chuantu.deleget = self;
        chuantu.isyujudian = self.isYuJuDian;
        chuantu.dId = self.ID;

        [self.navigationController pushViewController:chuantu animated:YES];
    }
}


-(void)upLoadeImageProgress:(CGFloat)progress{
    
    [self hudShowWhithProgress:progress Imagerefresh:YES andComentRefrsh:NO];
}

-(MBProgressHUD *)MyHUD{
    
    if (!_MyHUD) {
        _MyHUD =  [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        _MyHUD.dimBackground = YES;
        _MyHUD.mode = MBProgressHUDModeDeterminate;
        _MyHUD.removeFromSuperViewOnHide = YES;

    }
    return _MyHUD;
}

-(void)hudShowWhithProgress:(CGFloat)progress Imagerefresh:(BOOL)Imagerefresh andComentRefrsh:(BOOL)commentRefresh{

    self.MyHUD.labelText = [NSString stringWithFormat:@"%.f%%",progress*100];
    self.MyHUD.progress = progress;
    
    if (progress==1) {
        
     
         [MBProgressHUD hideHUDForView:self.view animated:YES];
        self.MyHUD = nil;
        
        if (Imagerefresh) {
            [Manager GetHudWhithText:@"上传成功,待审核" AndFatherView:self.view];
        }
        
        if (commentRefresh) {
//             self.mytableview.upLoadImageFinish = @"finish";
        }
        
    }
}

-(void)upLoadfailure{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    self.MyHUD = nil;
    [Manager GetHudWhithText:@"上传失败" AndFatherView:self.view];
}
#pragma mark---点击点评
-(void)DianpingClick{
    
    if (![self.app.isLogin isEqualToString:@"YES"]) {
        
        LoginController *login = [LoginController new];
        [self.navigationController pushViewController:login animated:YES];
        
    }else{
        
        ClickCommentCtl *ctrl = [ClickCommentCtl new];
        ctrl.deleget = self;
        ctrl.Id = self.ID;
        if (!self.isYuJuDian) {
            ctrl.url = islanComment;
        }else{
            
            ctrl.url = dianpuComment;
        }
   
        ctrl.isyujudian = self.isYuJuDian;
        [self.navigationController pushViewController:ctrl animated:YES];
    }
}
-(void)Finish{

    [Manager GetHudWhithText:@"点评成功,待审核" AndFatherView:self.view];
}

-(void)CommentupLoadeImageProgress:(CGFloat)progress{
    
    [self hudShowWhithProgress:progress Imagerefresh:NO andComentRefrsh:YES];
}

-(void)CommentupLoadfailure{

    [MBProgressHUD hideHUDForView:self.view animated:YES];
    self.MyHUD = nil;
    [Manager GetHudWhithText:@"上传失败" AndFatherView:self.view];

}
#pragma mark---点击报错
-(void)BaocuoClick{
    
    if (![self.app.isLogin isEqualToString:@"YES"]) {
        LoginController *login = [LoginController new];
        [self.navigationController pushViewController:login animated:YES];
    }else{
    UIAlertController *aler = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *To1 = [UIAlertAction actionWithTitle:self.yiGuan style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self WoringUploadWhithText:self.yiGuan];
    }];
    UIAlertAction *To2 = [UIAlertAction actionWithTitle:@"地理位置错误" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self WoringOfLocal];
    }];
    UIAlertAction *To3 = [UIAlertAction actionWithTitle:self.xinxiYouwu style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {//钓场信息有误
        [self ToBianji];
    }];
    UIAlertAction *To4 = [UIAlertAction actionWithTitle:self.chongfu style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
         [self WoringUploadWhithText:self.chongfu];
    }];
    UIAlertAction *To5 = [UIAlertAction actionWithTitle:@"其他" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        IslanOtherWrongController *other = [IslanOtherWrongController new];
        other.tid = self.ID;
        
        if (self.isYuJuDian) {
            other.module = @"dianpu";
        }else{
            other.module = @"diaochang";
        }
        [self.navigationController pushViewController:other animated:YES];
        
    }];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    
    [aler addAction:To1];
    [aler addAction:To2];
    [aler addAction:To3];
    [aler addAction:To4];
    [aler addAction:To5];
    [aler addAction:cancel];
    [self presentViewController:aler animated:YES completion:nil];
        
    }
}

#pragma mark--地理位置错误
-(void)WoringOfLocal{
    ChangeLocalCtrl *change = [ChangeLocalCtrl new];
    change.ChangeBlobk = ^(NSString *localname,NSString *latitude,NSString *longitute,NSString *cityPinY,NSString *cityCode){
        
        [[Manager sharedManager]RequestWhithURL:GetFormhash AndParameter:nil AndCompletionType:^(id responseObject, DIEError *error) {
            if ([responseObject[@"status"] isEqualToString:@"SUCCESS"]) {
                
                NSDictionary *dic = @{
                                      @"formhash":responseObject[@"data"][@"formhash"],
                                      @"d_id":self.ID,
                                      @"location":localname,
                                      @"longitude":longitute,
                                      @"latitude":latitude,
                                      };

                [self WoringlocaluploadWhithDic:dic];
            }
        }];
        
    };
    [self.navigationController pushViewController:change animated:YES];

}

-(void)WoringlocaluploadWhithDic:(NSDictionary *)dic{
    
    NSString *url = [NSString stringWithFormat:WoringLocal,@"diaochang"];
    if (self.isYuJuDian) {
        url = [NSString stringWithFormat:WoringLocal,@"dianpu"];
    }
    
 
    [[Manager sharedManager]ResponseWhithURL:url AndParameter:dic AndCompletionType:^(id responseObject, DIEError *error) {
        if ([responseObject[@"status"] isEqualToString:@"SUCCESS"]) {
        
            [Manager GetHudWhithText:@"提交成功" AndFatherView:self.view];
        }else{
            [Manager GetHudWhithText:@"提交失败" AndFatherView:self.view];
        }
    }];

}
#pragma mark--错误提交（钓场已关，钓场重复）
-(void)WoringUploadWhithText:(NSString *)text{
    
    NSString *module;
    NSString *idtype;
    if (self.isYuJuDian) {
        module = @"dianpu";
        idtype = @"y_id";
    }else{
        module = @"diaochang";
        idtype = @"d_id";
    }

    [[Manager sharedManager]RequestWhithURL:GetFormhash AndParameter:nil AndCompletionType:^(id responseObject, DIEError *error) {
        if ([responseObject[@"status"] isEqualToString:@"SUCCESS"]) {
            
            NSDictionary *dic = @{
                                  @"formhash":responseObject[@"data"][@"formhash"],
                                  idtype:self.ID,
                                  @"comment":text,
                                  };
            
            [[Manager sharedManager]ResponseWhithURL:[NSString stringWithFormat:islanOtherWoring,module] AndParameter:dic AndCompletionType:^(id responseObject, DIEError *error) {
                
                if (!error) {
                    
                    [Manager GetHudWhithText:responseObject[@"message"] AndFatherView:self.view];
                }else{
                    [Manager GetHudWhithText:@"提交失败" AndFatherView:self.view];
                }
                
            }];
            
        }else{
            [Manager GetHudWhithText:@"提交失败" AndFatherView:self.view];
        }
    }];

}
#pragma mark---点报错的 钓场信息有误
-(void)ToBianji{
   
    IslanInforChangeCtl *change = [IslanInforChangeCtl new];
    change.isYuJuDian = self.isYuJuDian;
    change.ID = self.ID;
    if (self.isYuJuDian) {
        change.number = 20;
        change.module = @"dianpu";
    }else{
        change.number = 10;
        change.module = @"diaochang";
    }
    
    [self.navigationController pushViewController:change animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self CreatTableview];
    [self CreatFootView];
}

-(void)CreatTableview{
    self.automaticallyAdjustsScrollViewInsets = NO;
    IsLanDescribTabView *tableView = [[IsLanDescribTabView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64-50) style:UITableViewStyleGrouped];
    [self.view addSubview:tableView];
    
    if (self.isYuJuDian) {
        tableView.module = @"dianpu";
    }else{
        tableView.module = @"diaochang";
    }
    

    tableView.latitude = self.latitude;
    tableView.longitude = self.longitude;
    tableView.isYuJuDian = self.isYuJuDian;
    tableView.Id = self.ID;
    tableView.Maindeleget = self;
    self.mytableview = tableView;
}

#pragma mark---点击详细地址,跳到地图页面显示
-(void)ClickDiaochangLocation:(NSString *)local AndName:(NSString *)name AndChargetype:(NSString *)chargeType{
    
    
    
    IslanDetailedLocalCtrl *DetailedLocal = [IslanDetailedLocalCtrl new];
    [self.navigationController pushViewController:DetailedLocal animated:YES];
    
    DetailedLocal.localString = local;
    DetailedLocal.chargeType = chargeType;
    DetailedLocal.name = name;
}
#pragma mark--点击查看更多评论或发表第一条评论
-(void)ComentActionWhithText:(NSString *)text AndModelAry:(NSArray *)dataAry{

    if ([text rangeOfString:@"1"].length!=0) {
        [self DianpingClick];
    }else if ([text rangeOfString:@"更多评论"].length!=0){
        IslanShowMoreCommentCtrl *more = [IslanShowMoreCommentCtrl new];
        more.dataAry = dataAry;
        [self.navigationController pushViewController:more animated:YES];
    }
}

#pragma mark--点击评论上的头像与名字 查看个人空间
-(void)ShowPersonSpaceWhithId:(NSString *)personId{
    OtherInformationCtrl *space = [OtherInformationCtrl new];
    space.uid = personId;
    [self.navigationController pushViewController:space animated:YES];
}

#pragma mark--去登录页面
-(void)GotoLogin{
    LoginController *login = [LoginController new];
    [self.navigationController pushViewController:login animated:YES];
}
#pragma mark--点击查看更多图片
-(void)MoreImageToShow:(NSArray *)imageAry{

    IslanShowMoreCtrl *more = [IslanShowMoreCtrl new];
    more.imageAry = imageAry;
    more.isImage = YES;
    [self.navigationController pushViewController:more animated:YES];
}
#pragma mark--点击查看简介
-(void)ShowDescrible:(NSString *)text{
    
    IslanShowMoreCtrl *more = [IslanShowMoreCtrl new];
    more.text = text;
    [self.navigationController pushViewController:more animated:YES];
}

#pragma mark--附近钓场点击
-(void)IsLanTabClickWhithDetail:(NSString *)detail AndLatitude:(NSString *)latitude AndLongitude:(NSString *)longitude AndLocaltion:(NSString *)localtion AndID:(NSString *)Id{

    IslanDescribCtll *desCreib = [IslanDescribCtll new];
    desCreib.isYuJuDian = self.isYuJuDian;
    desCreib.detail = detail;
    desCreib.latitude = latitude;
    desCreib.longitude = longitude;
    desCreib.localtion = localtion;
    desCreib.ID = Id;
    [self.navigationController pushViewController:desCreib animated:YES];
}


@end